<?php

class Users_model extends CI_Model
{

    private $_table = 'sm_users';
    private $_tbl_ruser = 'sm_users_role';

    public function __construct()
    {
        parent::__construct();
    }

    public function getAll($conditions = array(), $conditionFilter = array())
    {
        if (count($conditions) > 0)
            $this->db->where($conditions);

        if (count($conditionFilter) > 0)
            $this->db->or_like($conditionFilter);

        $this->db->from($this->_table);
        $this->db->join($this->_tbl_ruser, "{$this->_tbl_ruser}.userid = {$this->_table}.userid", 'left');
        $this->db->select('*');
        $result = $this->db->get();

        return $result;
    }

    public function getInfo($id)
    {
        $this->db->where($this->_table . '.userid', $id);
        $this->db->join($this->_tbl_ruser, "{$this->_tbl_ruser}.userid = {$this->_table}.userid", 'left');
        $query = $this->db->get($this->_table);
        return $query->row();
    }

    public function checkUsername($username, $email = '')
    {
        $this->db->where('username', $username);
        if (!empty($email)) {
            $this->db->or_where('email', $email);
        }
        $this->db->join('sm_users_role', 'sm_users_role.userid = ' . $this->_table . '.userid');
        $this->db->join('sm_role', 'sm_role.role_id = sm_users_role.role_id');
        $query = $this->db->get($this->_table);
        return $query->row();
    }

    /*
     * Create user
     */

    function createUser($insertData = array())
    {
        $this->db->insert($this->_table, $insertData);
        $last = $this->db->insert_id();
        return $last;
    }

    /*
     * View user
     */

    function viewUser($conditions = array(), $fields = '', $like = array(),
            $limit = array(), $orderby = array(), $conditionFilter = array())
    {
        $fields = '';

        //Check For Conditions
        if (is_array($conditions) and count($conditions) > 0)
            $this->db->where($conditions);

        //Check For Conditions Filter
        if (count($conditionFilter) > 0)
            $this->db->or_like($conditionFilter);

        //Check For like statement
        if (is_array($like) and count($like) > 0)
            $this->db->like($like);

        //Check For Limit
        if (is_array($limit)) {
            if (count($limit) == 1)
                $this->db->limit($limit [0]);
            else if (count($limit) == 2)
                $this->db->limit($limit [0], $limit [1]);
        }

        //Check for Order by
        if (is_array($orderby) and count($orderby) > 0)
            $this->db->order_by($orderby [0], $orderby [1]);

        $this->db->from("{$this->_table} u");
        $this->db->join("{$this->_tbl_ruser} r", "r.userid = u.userid", 'left');
        $this->db->select("u.*, r.role_id");
        $result = $this->db->get();
        return $result->result();
    }

    /*
     * Delete user
     */

    function deleteUser($id)
    {
        $user = (array) $this->getInfo($id);
        $result = '';
        if (count($user)) {
            //unlink Avatar
            global $CFG;
            $folder = $CFG->item('folder_avatar');
            if (file_exists($folder . $user['avatar']) && !empty($user['avatar'])) {
                unlink($folder . $user['avatar']);
            }

            $condition = array('userid' => $id);
            $this->role_model->deleteRoleUser($condition);
            $this->db->where($condition);
            $result = $this->db->delete($this->_table);
        }

        return $result;
    }

    /*
     * Update user
     */

    function updateUser($updateKey = array(), $updateData = array(), $typeWhere = '')
    {
        if ($typeWhere == 'IN') {
            $key = array_keys($updateKey);
            $this->db->where_in($key[0], $updateKey[$key[0]]);
        } else
            $this->db->where($updateKey);

        $result = $this->db->update($this->_table, $updateData);
        return $result;
    }

}
